package spring_07_transaction.example.service.impl;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import spring_07_transaction.example.dao.AccountDao;
import spring_07_transaction.example.service.AccountService;

public class AccountServiceImpl implements AccountService{

	private AccountDao accountDao;
	private TransactionTemplate transactionTemplate;
	
	public void setAccountDao(AccountDao accountDao){
		this.accountDao=accountDao;
	}
	
	public void setTransactionTemplate(TransactionTemplate transactionTemplate){
		this.transactionTemplate=transactionTemplate;
	}
	
	@Override
	public void transfer(final String outer,final String inner,final int money) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				accountDao.out(outer, money);
                //int i = 1/0;
                accountDao.in(inner, money);
			}
		});
	}
	
	

}
